package com.voice.call.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.voice.call.entity.LuckyCreditStar;
import com.voice.call.entity.LuckyCreditUser;
import com.voice.call.mapper.LuckyCreditUserMapper;
import com.voice.call.service.LuckyCreditStarService;
import com.voice.call.service.LuckyCreditUserService;

@Service
public class LuckyCreditUserServiceImpl implements LuckyCreditUserService{
	
	@Resource
	private LuckyCreditUserMapper luckyCreditUserMapper;
	
	@Resource
	private LuckyCreditStarService luckyCreditStarService;

	@Override
    public	LuckyCreditUser getLuckyCreditUserForId(Integer user_id){
		
		LuckyCreditUser entity = luckyCreditUserMapper.get(user_id);
		if (entity == null) {
			entity = new LuckyCreditUser();
			entity.setUserId(user_id);
			entity.setStarNum(2);
			entity.setCtime(new Date());
			entity.setMtime(new Date());
			if (luckyCreditUserMapper.insert(entity)>0) {
				return entity;
			}
		}
		return entity;
	}
	
	//获取额外积分
	@Override
	public	Integer getExtraLuckyCreditIntegral(Integer user_id){
		LuckyCreditUser entity = luckyCreditUserMapper.get(user_id);
		if (entity!=null) {
			
			LuckyCreditStar star = luckyCreditStarService.getLuckyCreditStarForStarNum(entity.getStarNum());
			if (star!=null) {
				return star.getIntegral();
			}
		}
		return 0;
	}
	
	//修改心数量
	@Override
	public	boolean updateStarNum(Integer user_id,Integer starNum){
		LuckyCreditUser entity = luckyCreditUserMapper.get(user_id);
		if (entity == null) { //不存在，则新增
			entity = new LuckyCreditUser();
			entity.setUserId(user_id);
			entity.setStarNum(starNum);
			entity.setCtime(new Date());
			entity.setMtime(new Date());
			if (luckyCreditUserMapper.insert(entity)>0) {
				return true;
			}
			return false;
		}
		//存在则修改
		LuckyCreditUser updateLuckyCreditUser = new LuckyCreditUser();
		updateLuckyCreditUser.setUserId(user_id);
		updateLuckyCreditUser.setStarNum(starNum);
		updateLuckyCreditUser.setMtime(new Date());
		if (luckyCreditUserMapper.update(updateLuckyCreditUser)>0) {
			return true;
		}
		return false;
	}
}
